//
//  DDMyQuestionDetailCollectionReusableView.m
//  dingdang
//
//  Created by Chen Jing on 15-7-28.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDMyQuestionDetailCollectionReusableView.h"
#import "UIImageView+WebCache.h"
#import "DateUtil.h"
#import "DDTypeDefine.h"

@interface DDMyQuestionDetailCollectionReusableView (){
    CGRect holdRect_labelCourseFrame;
    CGRect holdRect_labelRewardFrame;
    CGRect holdRect_imageViewGold;
}


@end

@implementation DDMyQuestionDetailCollectionReusableView

- (void)awakeFromNib {
    NSLog(@"进入到---------->DDMyQuestionDetailCollectionReusableView");
    // Initialization code
    self.imageView_head.layer.cornerRadius = CGRectGetWidth(self.imageView_head.frame) / 2.0;
    self.imageView_head.layer.masksToBounds = YES;
    UITapGestureRecognizer * tapOnImageBody = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapOnImage_body)];
    [self.imageView_body addGestureRecognizer:tapOnImageBody];
}

-(void)fixView{
    
    if (holdRect_imageViewGold.origin.x == 0) {
        holdRect_imageViewGold = self.imageView_gold.frame;
        holdRect_labelCourseFrame = self.label_course.frame;
        holdRect_labelRewardFrame = self.label_reword.frame;
    }
    
    
    self.imageView_gold.frame = holdRect_imageViewGold;
    self.label_reword.frame = holdRect_labelRewardFrame;
    self.label_course.frame = holdRect_labelCourseFrame;
    self.imageView_course.frame = CGRectMake(holdRect_labelCourseFrame.origin.x - 3, holdRect_labelCourseFrame.origin.y, holdRect_labelCourseFrame.size.width + 3, holdRect_labelCourseFrame.size.height);
    
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 1;
    
    
    if (self.info_detail) {
        NSRange timeRange = {0,10};

        float addHeight = 0.0;

        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 1;

        NSTimeInterval createTime = [[self.info_detail.info_createTime substringWithRange:timeRange] integerValue];
        NSDate * myDate = [NSDate dateWithTimeIntervalSince1970:createTime];
        self.label_date.text = [DateUtil format:@"YYYY-MM-dd" date:myDate];
        self.label_userName.text = self.info_element.info_userName;
        [self.imageView_head sd_setImageWithURL:[NSURL URLWithString:self.info_element.info_userAvatarUrl] placeholderImage:[UIImage imageNamed:@"个人中心-人数统计头像"]];
        if (self.info_detail.info_courseName) {
            self.label_course.text = self.info_detail.info_courseName;
        }


        if (self.info_element.info_answerNumber) {
            self.lable_answerPeopleNumber.text = [NSString stringWithFormat:@"%@人回答",self.info_element.info_answerNumber];
        }


        BOOL isHaveImage = NO;
        for (DDModelMyQuetionsListElement_answerInfo_body * tempBody in self.info_detail.info_body) {
            if ([tempBody.info_type isEqualToString:@"TEXT"]) {
                self.label_body.text = tempBody.info_text;
                CGRect tempRect = [tempBody.info_text boundingRectWithSize:CGSizeMake(self.label_body.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : self.label_body.font,NSParagraphStyleAttributeName : paragraphStyle} context:nil];

                float tempAddBody = tempRect.size.height - 21;
                addHeight += tempAddBody;

                self.label_body.frame = CGRectMake(CGRectGetMinX(self.label_body.frame),
                                                   61,
                                                   CGRectGetWidth(self.label_body.frame),

                                                   21 + tempAddBody);

            }else if ([tempBody.info_type isEqualToString:@"IMAGE"]){
                isHaveImage = YES;
                [self.imageView_body sd_setImageWithURL:[NSURL URLWithString:tempBody.info_url] placeholderImage:[UIImage imageNamed:@"首页-答疑-客观题-单选习题拍照图-通用"]];

            }
        }
        if (self.info_element.info_courseName) {
            self.label_course.text = self.info_element.info_courseName;
        }


        if (isHaveImage == YES) {
            self.imageView_body.frame = CGRectMake(8, 93 + addHeight, CGRectGetWidth([[UIScreen mainScreen] bounds])- 16, 100);
            addHeight += 100;
        }

        if (self.info_detail.info_typeName) {
            self.label_questionType.frame = CGRectMake(8, 100 + addHeight, 150, 20);
            self.label_questionType.text = [NSString stringWithFormat:@"(%@)",self.info_detail.info_typeName];
            addHeight += 30;
        }


        for (UIView * tempView in self.contentOfOptionsView.subviews) {
            [tempView removeFromSuperview];
        }

        NSInteger optionsCount = self.info_detail.info_options.count;
        CGFloat heightOfContentOptions = 55 * optionsCount;
//        addHeight += heightOfContentOptions;
//        self.contentOfOptionsView.frame = CGRectMake(0, 90 + addHeight, CGRectGetWidth([[UIScreen mainScreen] bounds]), heightOfContentOptions);

        float h1 = 0.0;
        float h2 = 0.0;
        
        for (NSInteger i = 0; i < optionsCount; i++) {
            DDModelMyQuetionsListElement_answerInfo_option * singleOption = self.info_detail.info_options[i];

            UIView * tempView = [[UIView alloc]initWithFrame:CGRectMake(8, 5  + h1, CGRectGetWidth([[UIScreen mainScreen] bounds]), 50)];
            [self.contentOfOptionsView addSubview:tempView];


            UILabel * label_label = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
            label_label.textAlignment = NSTextAlignmentCenter;
            label_label.layer.cornerRadius = 20;
            label_label.layer.masksToBounds = YES;
            label_label.backgroundColor = [UIColor lightGrayColor];
            label_label.font = [UIFont systemFontOfSize:16];
            label_label.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
            label_label.text = singleOption.info_label;
            [tempView addSubview:label_label];


            
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(50, 35, tempView.frame.size.width - 65, 20)];
            label.font = [UIFont systemFontOfSize:14];
            label.numberOfLines = 0;
            label.textColor = [UIColor colorWithRed:121/255.0 green:121/255.0 blue:121/255.0 alpha:1];
//            label.backgroundColor = [UIColor greenColor];
            [tempView addSubview:label];

            UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 0, 30, 30)];
            imageView.userInteractionEnabled = YES;
            imageView.tag = i;
            [tempView addSubview:imageView];
            UITapGestureRecognizer * tempTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapOnOptionImageWithTag:)];
            [imageView addGestureRecognizer:tempTap];

            BOOL isHaveText = NO;
            BOOL isHaveImage = NO;

            for (NSInteger j = 0; j < singleOption.info_content.count; j++) {
               DDModelMyQuetionsListElement_answerInfo_body * optionElement = singleOption.info_content[j];
                if ([optionElement.info_type isEqualToString:@"TEXT"]) {
                    isHaveText = YES;
                    label.text = checkNull(optionElement.info_text);
                }else if ([optionElement.info_type isEqualToString:@"IMAGE"]){
                    isHaveImage = YES;
                    [imageView sd_setImageWithURL:[NSURL URLWithString:optionElement.info_url] placeholderImage:[UIImage imageNamed:@"个人中心-人数统计头像"]];
                }
            }

            
            
            CGRect tempRect = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSParagraphStyleAttributeName : paragraphStyle} context:nil];
            label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, tempRect.size.height);
            if (tempRect.size.height < 55) {
                h1 += 55;
            }else{
                h1 += tempRect.size.height;
            }
            
            tempView.frame = CGRectMake(tempView.frame.origin.x, tempView.frame.origin.y, tempView.frame.size.width, tempRect.size.height + 30);
            
            
            if (isHaveText == YES && isHaveImage == NO) {
                label.frame = CGRectMake(50, 0, tempView.frame.size.width - 65, label.frame.size.height);
                imageView.hidden = YES;
            }else if (isHaveText == NO && isHaveImage == YES){
                imageView.frame = CGRectMake(50, 0, 30, 30);
                label.hidden = YES;
            }else if (isHaveImage == NO && isHaveText == NO){
                label.hidden = YES;
                imageView.hidden = YES;
            }
        }
        
        self.contentOfOptionsView.frame = CGRectMake(0, 90 + addHeight, CGRectGetWidth([[UIScreen mainScreen] bounds]), heightOfContentOptions + h1);

        
        
        if ([self.info_element.info_type isEqualToString:@"2"]) {
            if (self.info_element.info_reward) {
                self.label_reword.text = self.info_element.info_reward;
            }else{
                self.label_reword.text = @"0";
            }
            
            self.imageView_gold.hidden = NO;
        }else{
            self.label_reword.hidden = YES;
            self.imageView_gold.hidden = YES;
            
        }

    }
    
    CGRect tempRectCourse = [self.label_course.text boundingRectWithSize:CGSizeMake(MAXFLOAT, holdRect_labelCourseFrame.size.height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.label_course.font,NSParagraphStyleAttributeName : paragraphStyle} context:nil];
    float addWith = tempRectCourse.size.width;
    if (addWith > CGRectGetWidth([[UIScreen mainScreen] bounds]) - 220) {
        addWith = CGRectGetWidth([[UIScreen mainScreen] bounds]) - 220;
    }else if (addWith < holdRect_labelCourseFrame.size.width){
        addWith = holdRect_labelCourseFrame.size.width;
    }
    
    self.label_course.frame = CGRectMake(holdRect_labelCourseFrame.origin.x + holdRect_labelCourseFrame.size.width - addWith, holdRect_labelCourseFrame.origin.y, addWith, holdRect_labelCourseFrame.size.height);
    self.imageView_course.frame = CGRectMake(self.label_course.frame.origin.x - 3, self.label_course.frame.origin.y, self.label_course.frame.size.width + 3, self.label_course.frame.size.height);
    UIImage * tempCourseImage = [self.imageView_course.image stretchableImageWithLeftCapWidth:floorf(self.imageView_course.image.size.width/2) topCapHeight:floorf(self.imageView_course.image.size.height/2)];
    self.imageView_course.image = tempCourseImage;
    
    self.label_reword.frame = CGRectMake(holdRect_labelRewardFrame.origin.x - addWith - 3 + holdRect_labelCourseFrame.size.width, holdRect_labelRewardFrame.origin.y, holdRect_labelRewardFrame.size.width, holdRect_labelRewardFrame.size.height);
    self.imageView_gold.frame = CGRectMake(holdRect_imageViewGold.origin.x - addWith - 3 + holdRect_labelCourseFrame.size.width, holdRect_imageViewGold.origin.y, holdRect_imageViewGold.size.width, holdRect_imageViewGold.size.height);
    
    
    
   
}

-(void)tapOnImage_body{
    if (self.delegate && [self.delegate respondsToSelector:@selector(imageViewClicked:)]) {
            for (DDModelMyQuetionsListElement_answerInfo_body * tempBody in self.info_detail.info_body) {
               if ([tempBody.info_type isEqualToString:@"IMAGE"]){
                   [self.delegate performSelector:@selector(imageViewClicked:) withObject:tempBody.info_url];
                   
                }
            }
        }
    
}

-(void)tapOnOptionImageWithTag:(UITapGestureRecognizer *)ges{
    if (self.delegate && [self.delegate respondsToSelector:@selector(imageViewClicked:)]) {
        NSInteger tag = ges.view.tag;
            if (tag < self.info_detail.info_options.count) {
                DDModelMyQuetionsListElement_answerInfo_option * singleOption = self.info_detail.info_options[tag];
                for (NSInteger j = 0; j < singleOption.info_content.count; j++) {
                    DDModelMyQuetionsListElement_answerInfo_body * optionElement = singleOption.info_content[j];
                    if ([optionElement.info_type isEqualToString:@"IMAGE"]){
                        [self.delegate performSelector:@selector(imageViewClicked:) withObject:optionElement.info_url];
                        
                    }
                }
            }
        
        }
    
}


@end
